<?php
class BuyOrderInfoService {
	
	public function removeGoodsById($user, $id) {
		$goods = $this->getOrderGoodsById ( $id );
		
		if (empty ( $goods )) {
			return array ('status' => false, 'message' => "产品不存在或已被删除" );
		}
		
		$rs = BuyOrderInfoModel::instance ()->delOrderGoodsById ( $id );
		
		//删除产品
		//更新产品线
		$flag = $this->saveOrderBySNO ( $goods ['orderno'] );
		if ($flag < 1) {
			return array ('status' => false, 'message' => "订单无法更新" );
		}
		
		$os = new BuyOrderService ();
		$order = $os->getOrderBySNO ( $goods ['orderno'] );
		$this->addLog ( $order ['id'], $user ['real_name'], "删除产品$goods[goods_name]" );
		return array ('status' => true, 'data' => $order, 'message' => "删除成功" );
	}
	
	public function setGoodsById($user, $key, $value, $id) {
		$info = $this->getOrderGoodsById ( $id );
		if (empty ( $info )) {
			return array ('status' => false, 'message' => "更改失败" );
		}
		$gService = new BuyService ();
		$goods = $gService->getGoodsById ( $info ['goods_id'] );
		
		if ($key == 'num') {
			$v = array ('num' => $value, 'total_money' => $info ['price'] * $goods ['num'] * $value );
		} else {
			$v = array ($key => $value, 'total_money' => $info ['num'] * $value * $goods ['num'] );
		}
		
		$rs = BuyOrderInfoModel::instance ()->setOrderInfoByID ( $v, $id );
		$this->saveOrderBySNO ( $info ['orderno'] );
		
		$os = new BuyOrderService ();
		$order = $os->getOrderBySNO ( $info ['orderno'] );
		$this->addLog ( $order ['id'], $user ['real_name'], "更新产品$goods[goods_name]" );
		
		return array ('status' => true, 'data' => array ('goods' => $v, 'order' => $order ), 'message' => "更改成功" );
	
	}
	//更新原订单数量和总金额
	private function saveOrderBySNO($ordersno) {
		$order = new BuyOrderService ();
		$rs = $this->getGoodsBySNO ( $ordersno );
		foreach ( $rs as $v ) {
			$num += $v ['num'];
			$money += $v ['total_money'];
		}
		//更新财务信息
		$this->saveAccountBySNO ( $money, $ordersno );
		return $order->setOrderByOrderSNO ( array ('num' => $num, 'money' => $money ), $ordersno );
	}
	
	public function getOrderGoodsById($id) {
		return BuyOrderInfoModel::instance ()->getGoodsById ( $id );
	}
	
	//清理
	public function getOrderGoods($sno, $user) {
		//检查这个订单是否是属于自己的
		$orderService = new BuyOrderService ();
		$order = $orderService->getOrderBySNO ( $sno );
		
		if ($order ['uid'] != $user ['uid'] && $user ['manager'] != 1) {
			return array ('status' => fasle, 'message' => "你没有权限查看" );
		}
		
		$rs = BuyOrderInfoModel::instance ()->getGoodsBySNO ( $sno );
		//获取收件人
		$shr = $this->getShr ( $order ['uid'] );
		
		$goods = $this->getGoodsInfo ( $rs );
		
		//获取操作历史
		$ls = new BuyLogService ();
		$log = $ls->getLogByOrderId ( $order ['id'] );
		
		return array ('status' => true, 'data' => $goods, 'log' => $log, 'order' => $order, 'shr' => $shr );
	}
	
	public function saveSnapshot($snapshot, $id) {
		return BuyOrderInfoModel::instance ()->saveSnapshotById ( $snapshot, $id );
	}
	
	//追加
	public function append($user, $goodsName, $num, $orderno) {
		
		if (empty ( $num )) {
			return array ('status' => false, 'message' => "数量不能为空" );
		}
		
		$service = new BuyService ();
		$goods = $service->getGoodsByName ( trim ( $goodsName ) );
		if (empty ( $goods )) {
			return array ('status' => false, 'message' => "添加失败，没有找到关联产品" );
		}
		$totalMoney = $goods ['price'] * $goods ['num'] * $num;
		$cart = array ('goods_id' => $goods ['id'], 'goods_name' => $goods ['goods_name'], 'price' => $goods ['price'], 'orderno' => $orderno, 'num' => $num, 'total_money' => $totalMoney );
		$flag = $this->add ( $cart );
		if ($flag < 1) {
			return array ('status' => false, 'message' => "添加失败，没有新增" );
		}
		
		//更新订单
		$cart ['id'] = $flag;
		$cart ['num'] = $goods ['num'];
		$cart ['count'] = $num;
		$cart ['type'] = $goods ['type'];
		$this->saveOrderBySNO ( $orderno );
		$os = new BuyOrderService ();
		$order = $os->getOrderBySNO ( $orderno );
		$this->addLog ( $order ['id'], $user ['real_name'], "增加产品$goods[goods_name]" );
		
		return array ('status' => true, 'data' => array ('goods' => $cart, 'order' => $order ), 'message' => "新增成功" );
	
	}
	
	/**
	 * 添加产品
	 */
	public function add($cart) {
		return BuyOrderInfoModel::instance ()->addGoods ( $cart );
	
	}
	
	public function getGoodsBySNO($orderno) {
		return BuyOrderInfoModel::instance ()->getGoodsBySNO ( $orderno );
	}
	
	public function setOrderInfoSNOBySNO($newsno, $oldsno) {
		return BuyOrderInfoModel::instance ()->setOrderInfoSNOBySNO ( $newsno, $oldsno );
	}
	
	public function addLog($orderid, $username, $event) {
		
		//检查订单状态
		$os = new BuyOrderService ();
		$order = $os->getOrderById ( $orderid );
		if ($order ['status'] < 0) {
			return;
		}
		
		$log = new BuyLogService ();
		$log->addLog ( $orderid, $username, $event );
	}
	/**
	 * 更新财务信息
	 * Enter description here ...
	 * @param unknown_type $money
	 * @param unknown_type $orderno
	 */
	private function saveAccountBySNO($money, $orderno) {
		$account = new BuyAccountService ();
		$account->setAccountMoneyBySNO ( - $money, $orderno );
	}
	
	private function getShr($uid) {
		$member = new MemberService ();
		return $member->getMemberByUid ( $uid );
	}
	
	private function getGoodsInfo($rs) {
		$goodsService = new BuyService ();
		foreach ( $rs as $k => $v ) {
			$goods = $goodsService->getGoodsById ( $v ['goods_id'] );
			$cate = $goodsService->getCateById ( $goods ['cid'] );
			$rs [$k] ['count'] = $v ['num'];
			$rs [$k] ['type'] = $goods ['type'];
			$rs [$k] ['num'] = $goods ['num'];
			$rs [$k] ['unit'] = $cate ['unit'];
			$rs [$k] ['module'] = $goods ['module'];
		}
		return $rs;
	}
}